package com.xunji.xunji.module.trace.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xunji.xunji.R;
import com.xunji.xunji.module.trace.bean.TagInfo;

import java.util.List;

/**
 * Created by Paul on 2017/2/14.
 */

public class TagAdapter extends BaseAdapter {
    private List<TagInfo> tagInfoList;

    private Context context;

    public TagAdapter(List<TagInfo> tagInfoList, Context context) {
        this.tagInfoList = tagInfoList;
        this.context = context;
    }

    public void setDataChanged(List<TagInfo> tagInfos) {
        this.tagInfoList = tagInfos;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return tagInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return tagInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            holder = new Holder();
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.view_trace_image_label, null);
            holder.textView = (TextView) convertView.findViewById(R.id.tv_text);
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_image);


            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        if (tagInfoList.get(position).isSelect()) {
            convertView.setBackgroundColor(context.getResources().getColor(R.color.main_color));
        } else {
            convertView.setBackgroundColor(context.getResources().getColor(R.color.white));
        }



        holder.textView.setText(tagInfoList.get(position).getTitle());

        holder.imageView.setImageResource(tagInfoList.get(position).getImageId());
        return convertView;
    }


    class Holder {
        TextView textView;
        ImageView imageView;
    }
}
